﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlamDunk.MES.Domain.Entities.FoundationContext
{
    /// <summary>
    /// 计量单位组
    /// </summary>
    public class MeasureUnitGroup : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected MeasureUnitGroup()
        {
            //初始化导航属性
            this.MeasureUnits = new HashSet<MeasureUnit>();
        }
        #endregion

        #region 01.创建计量单位组构造器
        /// <summary>
        /// 创建计量单位组构造器
        /// </summary>
        /// <param name="groupNo">计量单位组编号</param>
        /// <param name="groupName">计量单位组名称</param>
        /// <param name="description">描述</param>
        public MeasureUnitGroup(string groupNo, string groupName, string description)
            : this()
        {
            base.Number = groupNo;
            base.Name = groupName;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 主计量单位 —— MeasureUnit MasterUnit
        /// <summary>
        /// 只读属性 - 主计量单位
        /// </summary>
        public MeasureUnit MasterUnit
        {
            get { return this.MeasureUnits.Single(x => x.IsMaster); }
        }
        #endregion

        #region 导航属性 - 计量单位集 —— ICollection<MeasureUnit> MeasureUnits
        /// <summary>
        /// 导航属性 - 计量单位集
        /// </summary>
        public virtual ICollection<MeasureUnit> MeasureUnits { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改计量单位组 —— void UpdateInfo(string groupName, string description)
        /// <summary>
        /// 修改计量单位组
        /// </summary>
        /// <param name="groupName">计量单位组名称</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string groupName, string description)
        {
            base.Name = groupName;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置计量单位 —— void SetMeasureUnits(IEnumerable<MeasureUnit> measureUnits)
        /// <summary>
        /// 设置计量单位
        /// </summary>
        /// <param name="measureUnits">计量单位集</param>
        public void SetMeasureUnits(IEnumerable<MeasureUnit> measureUnits)
        {
            measureUnits = measureUnits?.ToArray() ?? new MeasureUnit[0];

            this.ClearMeasureUnits();
            if (measureUnits.Any())
            {
                this.MeasureUnits.AddRange(measureUnits);
                foreach (MeasureUnit measureUnit in measureUnits)
                {
                    measureUnit.MeasureUnitGroup = this;
                }
            }
        }
        #endregion

        #region 清空计量单位 —— void ClearMeasureUnits()
        /// <summary>
        /// 清空计量单位
        /// </summary>
        private void ClearMeasureUnits()
        {
            foreach (MeasureUnit measureUnit in this.MeasureUnits.ToArray())
            {
                this.MeasureUnits.Remove(measureUnit);
            }
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
